package legumes.client.ui.widgets.window;

import legumes.client.resources.PNResources;
import legumes.client.services.callbacks.SaveStoreCallback;
import legumes.client.services.util.rpc.RPCCallSequencer;
import legumes.client.ui.util.wrapper.LatLngWrapper;
import legumes.client.ui.widgets.form.item.PlacesDeliverySelectItem;
import legumes.shared.domain.PlaceDelivery;
import legumes.shared.domain.Store;

import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.WidgetCanvas;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class StorePlacesDeliveryEditionWindow extends Window {

	private Store store;
	private ImgButton addBtn;
	private ImgButton editBtn;
	private ImgButton delBtn;
	private PlacesDeliverySelectItem placesDeliverySi;
//	private PlacesDeliveryDetailsPanel pddp;
	private PlacesDeliveryMapPanel pdmp;
	
	public StorePlacesDeliveryEditionWindow(Store store_) {
		super();
		store = store_;
		setTitle("<img src=\"" + PNResources.INSTANCE.deliveryIcon().getURL() + "\" />&nbsp;Cr&eacute;&eacute;r une nouvelle adresse");
		
		int windowHeight = com.google.gwt.user.client.Window.getClientHeight();
		if(windowHeight>=580 ){
			setHeight( 580 );
		} else {
			setHeight( 480 );
		}
		setWidth(640);
		setShowCloseButton(true);
		setShowMinimizeButton(false);
		setAutoCenter(true);
		setIsModal(true);
		setShowModalMask(true);
		addCloseClickHandler(new CloseClickHandler() {  
            public void onCloseClick(CloseClientEvent event) {  
            	StorePlacesDeliveryEditionWindow.this.destroy();  
            }  
        });

		addItem( buildPanel() );
		show();
		placesDeliverySi.focusInItem();
	}

	
	private void buildButtons(){
		int imgButtonSize = 16;
		addBtn = new ImgButton();
		addBtn.setSize(imgButtonSize);
		addBtn.setShowRollOver(false);  
		addBtn.setSrc(PNResources.INSTANCE.addIcon().getURL());
		addBtn.setTooltip("Créer un nouveau lieu de livraison...");
		addBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				onAddAction(null);
			}
		});
		
		editBtn = new ImgButton();
		editBtn.setSize(imgButtonSize);
		editBtn.setShowRollOver(false);
		editBtn.setShowSelectedIcon(false);
		editBtn.setShowDownIcon(false);
		editBtn.setShowFocusedIcon(false);
		editBtn.setShowDisabledIcon(false);
		editBtn.setSrc(PNResources.INSTANCE.editIcon().getURL());
		editBtn.setTooltip("Modifier le lieu de livraison sélectionné...");
		editBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				onEditAction(placesDeliverySi.getSelectedPlaceDelivery());
			}
		});

		delBtn = new ImgButton();
		delBtn.setSize(imgButtonSize);
		delBtn.setShowRollOver(false);  
		delBtn.setSrc(PNResources.INSTANCE.deleteIcon().getURL());
		delBtn.setTooltip("Supprimer le lieu de livraison sélectionné...");
		delBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				onDelAction(placesDeliverySi.getSelectedPlaceDelivery());
			}
		});
	}
	
	protected void onAddAction(PlaceDelivery selectedPlaceDelivery_) {
		createStoreNewPlaceDeliveryEditionWindow(selectedPlaceDelivery_);
	}


	protected void onEditAction(PlaceDelivery selectedPlaceDelivery_) {
		createStoreNewPlaceDeliveryEditionWindow(selectedPlaceDelivery_);
	}
	
	private void createStoreNewPlaceDeliveryEditionWindow(final PlaceDelivery selectedPlaceDelivery_){
		new StoreNewPlaceDeliveryEditionWindow(store,
				PlaceDelivery.createPlaceDeliveryNameArray(store.getPlaceDeliveryList()),
				selectedPlaceDelivery_){

					@Override
					protected void onStoreSaved(Store store_, PlaceDelivery modifiedPlaceDelivery_) {
						super.onStoreSaved(store_, modifiedPlaceDelivery_);
						placesDeliverySi.refreshStoreList(store_.getPlaceDeliveryList(),
								modifiedPlaceDelivery_.getTitle());
						pdmp.refreshStoreList();
						pdmp.onPlaceDeliverySelected(modifiedPlaceDelivery_);
						pdmp.getMapWidget().setCenter(LatLngWrapper.converLatLng(modifiedPlaceDelivery_.getLatLng()));
					}
			
		};
	}


	protected void onDelAction(final PlaceDelivery selectedPlaceDelivery) {
		SC.confirm("Supprimer un lieu de livraison", 
				"Souhaitez vous réelement supprimer le lieux de livraison \"" + selectedPlaceDelivery.getTitle() + "\"", 
				new BooleanCallback() {
			
			@Override
			public void execute(Boolean value) {
				if(value ){
					store.removePlaceDelivery(selectedPlaceDelivery);
					saveOnServer();
				}
			}
		});
	}

	private VLayout buildPanel() {
		VLayout mainVLayout = new VLayout();
		//north toolbar
		HLayout toolbar = buildToolbar();
		
		//center Map
		pdmp = new PlacesDeliveryMapPanel(650, 550, null, store.getPlaceDeliveryList()){

			@Override
			protected void onPlaceDeliverySelected(PlaceDelivery pl_) {
				super.onPlaceDeliverySelected(pl_);
				if(pl_!=null){
					placesDeliverySi.setSelectedPlaceDelivery(pl_);
				}
			}
			
		};
		
		mainVLayout.setMembers(toolbar, new WidgetCanvas(pdmp));
		return mainVLayout;
	}

	private HLayout buildToolbar() {
		buildButtons();
		placesDeliverySi = new PlacesDeliverySelectItem(store);
		placesDeliverySi.addChangedHandler(new ChangedHandler() {
			
			@Override
			public void onChanged(ChangedEvent event) {
				pdmp.onPlaceDeliverySelected( placesDeliverySi.getSelectedPlaceDelivery() );
			}
		});
		placesDeliverySi.setWidth(250);
		DynamicForm df = new DynamicForm();
		df.setFields(placesDeliverySi);
		
		HLayout mainPanel = new HLayout();
		mainPanel.setWidth100();
		mainPanel.setHeight(25);
		mainPanel.setMargin(5);
		
		mainPanel.setMembers(df, addBtn, editBtn, delBtn);
		
		return mainPanel;
	}

	private void saveOnServer(){
		RPCCallSequencer callSequencer = new RPCCallSequencer("Enregistrement en cours...");
		final SaveStoreCallback saveStoreCallback = new SaveStoreCallback(
				store);
		callSequencer.addCall(saveStoreCallback);
		callSequencer.addRunnable(new Runnable() {
			public void run() {
				pdmp.refreshStoreList();
				placesDeliverySi.refreshStoreList(store.getPlaceDeliveryList(), null);
			}
		});
		callSequencer.callSequence();
	}

}
